//
//  HSYHud.m
//  HSY
//
//  Created by 陈浩 on 2017/4/1.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYHud.h"


@interface MBProgressHUD (HSY)

+ (NSUInteger)hsy_hideAllHUDsForView:(UIView *)view animated:(BOOL)animated;

@end

@implementation MBProgressHUD (HSY)

+ (NSUInteger)hsy_hideAllHUDsForView:(UIView *)view animated:(BOOL)animated{
    NSMutableArray *huds = [NSMutableArray array];
    NSArray *subviews = view.subviews;
    for (UIView *aView in subviews) {
        if ([aView isKindOfClass:self]) {
            [huds addObject:aView];
        }
    }
    for (MBProgressHUD *hud in huds) {
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:animated];
    }
    return [huds count];
}

@end

@implementation HSYHud

+(MBProgressHUD *)showHUDInView:(UIView *)view title:(NSString *)title{
    [MBProgressHUD hsy_hideAllHUDsForView:view animated:YES];
    
    MBProgressHUD *HUDInView = [MBProgressHUD showHUDAddedTo:view animated:YES];
    HUDInView.removeFromSuperViewOnHide = YES;
    HUDInView.label.text = title;
    HUDInView.square = YES;
    HUDInView.mode = MBProgressHUDModeIndeterminate;
    HUDInView.layer.cornerRadius=4.0f;
    HUDInView.layer.masksToBounds=YES;
    HUDInView.alpha=1.0f;
    HUDInView.backgroundColor = [UIColor clearColor];
    HUDInView.offset = CGPointMake(HUDInView.offset.x, 5);
    HUDInView.label.font=[UIFont boldSystemFontOfSize:14.0f];
    
    return HUDInView;
}

+(void)hideHUDInView:(UIView *)view{
    // 允许TableView滚动
    if ([view respondsToSelector:@selector(setScrollEnabled:)]) {
        [(UIScrollView*)view setScrollEnabled:YES];
    }
    
    [MBProgressHUD hsy_hideAllHUDsForView:view animated:NO];
}

+(MBProgressHUD *)showNetWorkErrorInView:(UIView *)view{
    // 允许TableView滚动
    if ([view respondsToSelector:@selector(setScrollEnabled:)]) {
        [(UIScrollView*)view setScrollEnabled:YES];
    }
    [MBProgressHUD hsy_hideAllHUDsForView:view animated:NO];
    MBProgressHUD *HUDInView = [MBProgressHUD showHUDAddedTo:view animated:NO];
    HUDInView.removeFromSuperViewOnHide = YES;
    HUDInView.mode = MBProgressHUDModeText;
    HUDInView.label.text = @"网络错误";
    
    [HUDInView hideAnimated:YES afterDelay:0.5];
    return HUDInView;
}

+(MBProgressHUD *)showTipInView:(UIView *)view title:(NSString*)title{
    // 允许TableView滚动
    if ([view respondsToSelector:@selector(setScrollEnabled:)]) {
        [(UIScrollView*)view setScrollEnabled:YES];
    }
    [MBProgressHUD hsy_hideAllHUDsForView:view animated:NO];
    MBProgressHUD *HUDInView = [MBProgressHUD showHUDAddedTo:view animated:NO];
    HUDInView.removeFromSuperViewOnHide = YES;
    HUDInView.mode = MBProgressHUDModeText;
    HUDInView.label.text = title;
    
    [HUDInView hideAnimated:YES afterDelay:0.5];
    return HUDInView;
}

+(MBProgressHUD *)showMessageInView:(UIView *)view title:(NSString *)title{
    // 允许TableView滚动
    if ([view respondsToSelector:@selector(setScrollEnabled:)]) {
        [(UIScrollView*)view setScrollEnabled:YES];
    }
    [MBProgressHUD hsy_hideAllHUDsForView:view animated:NO];
    MBProgressHUD *HUDInView = [MBProgressHUD showHUDAddedTo:view animated:NO];
    HUDInView.removeFromSuperViewOnHide = YES;
    HUDInView.mode = MBProgressHUDModeText;
    HUDInView.detailsLabel.text = title;
    [HUDInView hideAnimated:YES afterDelay:2.0];
    return HUDInView;
}


@end
